package com.example.utils.reflect;

import java.lang.reflect.Field;

/**
 * @Classname ReflectUtils
 * @Description TODO
 * @Date 2022/7/30 12:22
 * @Created by XiongXiong
 * @Author: X.I.O
 */
public class ReflectUtils {

    public static Field getFieldByName(Field[] fields,String fieldName){
        if(fields == null|| fields.length == 0){
            return null;
        }
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            if(field.getName().equals(fieldName)){
                return field;
            }
        }
        return null;
    }

    public static <T> Object getFieldValue(Field field,T  t){
        try {
            field.setAccessible(true);
            return field.get(t);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Object getNewInstance(Class< ? > cls){
        if(cls == null){
            return null;
        }
        try {
            return cls.newInstance();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
